package com.example.lte.api;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.example.lte.config.BusinessLogic;
import com.example.lte.config.SysBusinessUnitEnum;
import com.example.lte.entity.MenuEntity;
import com.example.lte.po.MenuReq;
import com.example.lte.po.MenuWhere;
import com.example.lte.po.req.MenuAddReq;
import com.example.lte.po.resp.MenuResp;
import com.example.lte.service.MenuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;

/**
 * @author lzh
 * @date 2020/11/17 - 14:46
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单模块")
public class MenuController {

    private MenuServiceImpl menuService;

    @Autowired
    public void setMenuService(MenuServiceImpl menuService) {
        this.menuService = menuService;
    }


    /**
     * {
     * "code": 0,
     * "msg": "操作成功",
     * "data": {
     * "content": [
     * {
     * "menuId": 2,
     * "note": "系统设置",
     * "menuSequence": 2,
     * "parentMenuId": 0,
     * "menuLevel": 1,
     * "hasChild": 1,
     * "isMenu": 1,
     * "routerName": "",
     * "iconType": "setting",
     * "menuKey": "sysManage",
     * "routerPath": "",
     * "routerComponent": "",
     * "addTime": "2020-11-17 13:41:49",
     * "updateTime": "2020-11-17 13:41:49",
     * "version": 0
     * }
     * ],
     * "pageable": {
     * "sort": {
     * "sorted": true,
     * "unsorted": false,
     * "empty": false
     * },
     * "offset": 0,
     * "pageNumber": 0,
     * "pageSize": 10,
     * "unpaged": false,
     * "paged": true
     * },
     * "totalPages": 1,
     * "last": true,
     * "totalElements": 1,
     * "number": 0,
     * "size": 10,
     * "sort": {
     * "sorted": true,
     * "unsorted": false,
     * "empty": false
     * },
     * "numberOfElements": 1,
     * "first": true,
     * "empty": false
     * },
     * "timestamp": 1605598954687
     * }
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询菜单信息", notes = "详细描述-分页查询菜单表")
    @BusinessLogic(value = "分页-菜单列表", businessUnit = SysBusinessUnitEnum.MENU_UNIT)
    public Page<MenuEntity> findPage(MenuReq req) {
        MenuEntity u = new MenuEntity();
        u.setHasChild(req.getHasChild());
        u.setMenuLevel(req.getMenuLevel());
        u.setMenuId(req.getMenuId());
        u.setParentMenuId(req.getParentMenuId());
        u.setNote(req.getNote());
        Sort sort = Sort.by(Sort.Direction.ASC, "menuId");
        Pageable pageable = PageRequest.of(req.getPageNo(), req.getPageSize(), sort);
        return menuService.findByCondition(u, pageable);
    }

    @GetMapping("/list")
    @ApiOperation(value = "动态查询菜单信息", notes = "详细描述-动态查询菜单表")
    @BusinessLogic(value = "动态查询-菜单列表", businessUnit = SysBusinessUnitEnum.MENU_UNIT)
    public List<MenuEntity> findWhereList(MenuWhere req) {
        MenuEntity u = new MenuEntity();
        u.setHasChild(req.getHasChild());
        u.setMenuLevel(req.getMenuLevel());
        u.setMenuId(req.getMenuId());
        u.setParentMenuId(req.getParentMenuId());
        u.setNote(req.getNote());
        return menuService.findByWhere(u);
    }


    @GetMapping("/enum")
    public List<MenuResp> findMenuEnum() {
        List<MenuResp> respList = new ArrayList<>();
        MenuEntity u = new MenuEntity();
        u.setIsMenu(1L);
        List<MenuEntity> menuEntityList = menuService.findByWhere(u);
        if (CollectionUtil.isNotEmpty(menuEntityList)) {
            menuEntityList.forEach(a -> {
                respList.add(MenuResp.builder()
                        .menuId(a.getMenuId())
                        .note(a.getNote())
                        .build());
            });

        }
        return respList;
    }


    @DeleteMapping("/delMenuByIds")
    @ApiOperation(value = "删除1个或多个菜单", notes = "详细描述-删除1个或多个菜单")
    @BusinessLogic(value = "删除1个或多个菜单", businessUnit = SysBusinessUnitEnum.MENU_UNIT)
    public void delMenuByIds(@RequestParam Long[] ids) {
        menuService.delMenuByIds(Arrays.asList(ids));
    }


    @PostMapping("/add")
    @ApiOperation(value = "添加1个菜单", notes = "详细描述-添加1个菜单")
    @BusinessLogic(value = "添加1个菜单", businessUnit = SysBusinessUnitEnum.MENU_UNIT)
    public MenuEntity addOneMenuOrRouterInfo(@RequestBody MenuAddReq req) {
        return menuService.addOneMenuOrRouterInfo(req);
    }
}
